#include <iostream>

using std::cout;
using std::endl;

class RAII
{
public:
    //1、在构造函数中初始化资源
    RAII(int *pdata)
    : _pdata(pdata)
    {
        cout << "RAII(int *)" << endl;
    }

    //2、在析构函数中回收资源
    ~RAII()
    {
        cout << "~RAII()" << endl;
        if(_pdata)
        {
            delete _pdata;
            _pdata = nullptr;
        }
    }
    //3、提供若干访问资源的方法
    int *get()
    {
        return _pdata;
    }

    int *operator->()
    {
        return _pdata;
    }

    int &operator*()
    {
        return *_pdata;
    }

    void reset(int *pdata)
    {
        if(_pdata)
        {
            delete _pdata;
            _pdata = nullptr;
        }
        _pdata = pdata;
    }
    //4、不允许复制或者赋值
    RAII(const RAII &rhs) = delete;
    RAII &operator=(const RAII &rhs) = delete;
private:
    int *_pdata;
};

void test()
{
    RAII raii(new int(10));//堆空间
    raii.get();
    raii.operator->();
    (*raii);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

